<template>
  <div class="content clearfix">
    <div class="left">
      <router-link to="/"><img src="./../assets/logo-header.png" height="84" width="154" alt="logo"></router-link>
    </div>
    <div class="middle">
      <ul class="firstMenu clearfix" id="nav">
        <li v-for="(fItem,index) of nav.firstMenu" @mouseenter="childCheck( index )" @mouseleave="childCheck1()">
        <router-link :to="{name:fItem.router}">{{fItem.name}}</router-link>
          <ul v-show=" index == i " class="secondMenu clearfix">
            <li v-for="sItem of fItem.secondMenu">
            <router-link :to='{name:"ProductDet",params:{item:sItem.router}}'>{{sItem.name}}</router-link>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <div class="lang">
<!--       <a href="">繁</a><span>/</span>
      <a href="">简</a><span>/</span>
      <a href="">EN</a> -->
      <el-button type='text' @click="dialogVisible=true">管理员登录</el-button>
      <!-- <router-link to="/login" class="login">管理员登录</router-link> -->
      <div class="loginWrapper">
        <div class="bd">
          <el-dialog
            title="管理员登录" v-model="dialogVisible" size="tiny" @click="loginUser()">
            <div class='dialogDiv'>
              <el-form :model="loginForm" :rules="loginRule" ref="loginForm">
                <el-form-item prop="userName">
                  <el-input type="userName" v-model="loginForm.userName" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item prop="pwd">
                  <el-input v-model="loginForm.pwd" placeholder="密码" type="password"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('loginForm')" class="submitBtn">登录</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import jQuery from 'jquery'
export default {
  data () {
    return {
      nav: [],
      i: -1,
      ishow: false,
      dialogVisible: false,
      loginForm: {
        userName: '',
        pwd: ''
      },
      loginRule: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
        user() {
          return this.$store.state.user
        }
      },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var data = {
            'username': this.loginForm.userName,
            'password': this.loginForm.pwd
          }
          console.log(data.usr,data.pwd);
          this.$http.get('http://localhost:3000/users?username='+data.username+'&password='+data.password)
          .then(function (response) {
            console.log(response.body)
            if(response.body != null & response.body.length > 0){
              console.log(response.body[0]);
              this.$store.commit('isLogin',response.body[0]);
              this.loginForm.userName=''
              this.loginForm.pwd= ''
              this.$router.push({ path: 'login' })
            }else{
              console.log(response.body[0]);
              alert('请输入正确的用户名和密码！！！');
             his.loginForm.userName=''
              this.loginForm.pwd= ''
            }
          })
          .catch(function (error) {
            console.log(error);
          });
        } else {
          return false
        }
      })
    },
    childCheck: function(index) {
      this.i = index;
    },
    childCheck1: function() {
      this.i = -1;
    },
    getData () {
      this.$http({
        url: 'http://localhost:2000/getjson/nav',
        method: 'get'
      }).then((res) => {
        this.nav = res.data
      }).catch((res) => {
        console.log('error:', res)
      })
    }
  },
  created () {
    this.getData();
   },
  beforeUpdate () {
    let w = jQuery('body').width();
    let l = jQuery('.secondMenu').innerWidth() - jQuery('.secondMenu').width();
    let s = w-l;
    jQuery("#nav>li>ul").width(s);
  }
}
</script>

<style scoped>


@media screen and (max-width:1260px) {
  .left img {
    margin-right: 1rem !important;
  }
  .left img {
    display: none;
  }
  .firstMenu>li, .secondMenu>li {
    margin-right: 0.5rem !important;
  }
  .middle {
    width: 38rem  !important;
  }
}

.left, .middle, .lang{
  float: left;
}
.left {
  height: 84px;
}
.left img {
  margin-right: 2rem;
}
.middle {
  width: 40rem;
}
.firstMenu>li, .secondMenu>li {
  font-size: 0.8rem;
  float: left;
  font-weight: 800;
  width: 4.6rem;
  height: 84px;
  line-height: 84px;
  margin-right: 1rem;
  text-align: center;
}
.firstMenu>li:hover, .secondMenu>li:hover {
  border-bottom: 0.35rem solid #11a2d0;
}
.secondMenu {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  left: -0;
  padding-left: 22rem;
  background-color: rgba(255,255,255,0.8);
}
.secondMenu>li {
  width: 6.8rem;
  font-weight: normal;
}
.lang {
  float: right;
  height: 84px;
  line-height: 84px;
  font-size: 0.7rem;
  color: #99d7eb;
}
.lang>a {
  color: #99d7eb;
}
.login {
  display: inline-block;
  margin-left: 0.8rem;
}
</style>
